<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleApprove"
          v-hasPermi="['company:verify:approve']"
        >审核通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="single"
          @click="handleReject"
          v-hasPermi="['company:verify:reject']"
        >审核不通过</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="企业类型" align="center" prop="companyType"/>
      <el-table-column label="统一社会信用代码" align="center" prop="uniscid" />
      <el-table-column label="企业介绍" align="center" prop="intro" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="网址" align="center" prop="website" />
      <el-table-column label="注册类型" align="center" prop="registType">
        <template slot-scope="scope">
          <span v-if="scope.row.registType==1">
            批量导入
          </span>
          <span v-if="scope.row.registType==0">
            企业注册
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['company:verify:view']"
          >查看</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            v-hasPermi="['company:verify:approve']"
          >通过</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleReject(scope.row)"
            v-hasPermi="['company:verify:reject']"
          >不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 查看企业详情对话框 -->
    <el-dialog title="企业详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="企业名称">{{ form.companyName }}</el-descriptions-item>
        <el-descriptions-item label="企业类型">
         {{form.companyType}}
        </el-descriptions-item>
        <el-descriptions-item label="联系人">{{ form.contact }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ form.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="企业地址" :span="2">{{ form.address }}</el-descriptions-item>
        <el-descriptions-item label="企业简介" :span="2">{{ form.intro }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <template>
              <span style="color: red" v-if="form.status===0">
                未审核
              </span>
          </template>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button> <el-button @click="passCheck">关 闭</el-button> <el-button @click="refuseCheck">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="auditTitle" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="auditForm.status">
            <el-radio label="1">审核通过</el-radio>
            <el-radio label="2">审核不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { companyCheckList, auditCompany } from "@/api/biz/api";

export default {
  name: "CompanyVerify",
  dicts: ['company_type', 'audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      companyList: [],
      // 查看对话框
      viewOpen: false,
      // 审核对话框
      auditOpen: false,
      // 审核对话框标题
      auditTitle: "",
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        companyName: null,
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {
        companyId: null,
        status: "1",
        auditRemark: ""
      },
      // 审核表单校验
      auditRules: {
        status: [
          { required: true, message: "请选择审核结果", trigger: "change" }
        ],
        auditRemark: [
          { required: true, message: "请输入审核意见", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业审核列表 */
    getList() {
      this.loading = true;
      companyCheckList(this.queryParams).then(response => {
        this.companyList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });

    },
    // 取消按钮
    cancelAudit() {
      this.auditOpen = false;
      this.resetAudit();
    },
    // 审核表单重置
    resetAudit() {
      this.auditForm = {
        companyId: null,
        status: "1",
        auditRemark: ""
      };
      this.resetForm("auditForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = row;
      this.viewOpen = true;
    },
    /** 审核通过按钮操作 */
    handleApprove(row) {
      this.resetAudit();
      const companyId = row.id || this.ids[0];
      this.auditForm.companyId = companyId;
      this.auditForm.status = "1";
      this.auditTitle = "审核通过";
      this.auditOpen = true;
    },
    /** 审核不通过按钮操作 */
    handleReject(row) {
      this.resetAudit();
      const companyId = row.id || this.ids[0];
      this.auditForm.companyId = companyId;
      this.auditForm.status = "2";
      this.auditTitle = "审核不通过";
      this.auditOpen = true;
    },
    /** 提交审核 */
    submitAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditCompany(this.auditForm).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
